clear all
set more off



local J = `1'

local N = `2'




*Bootstrap

use flexlogitother

*limited z variation method

preserve 
bysort ID:egen stdz=sd(z)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(10)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
 forvalues i=1/14 {
 local alpha`i' = _b[x`i']
 }

restore

drop zdecile



clogit chosen x* z, group(ID)
forvalues i=1/3{
local naivex`i'=_b[x`i']
}
local naivez=_b[z]



*Create attributes of rival goods
preserve
keep ID altID x1 x2 x3 x4 x5 z

forvalues i=1/5{
rename x`i' x`i'_
}

reshape wide x* z, i(ID) j(altID)
tempfile attributes
save `attributes', replace
restore

merge m:1 ID using `attributes'
assert _merge == 3
drop _merge


*Create a new variable which is rival attributes for good with maxz
bys ID: egen maxz = max(z)


gen ismax = (maxz == z)
gen rn=runiform() if ismax==1
bysort ID:egen maxrn=max(rn)
gen ismaxrn=(rn==maxrn)
drop ismax
rename ismaxrn ismax
drop rn maxrn




local Jminus = `J'-1

gen maxID = altID if ismax == 1
bys ID: egen maxmaxID = max(maxID)
drop maxID
rename maxmaxID maxID




gen vijnaive=0

forvalues i=1/14{
 replace vijnaive=vijnaive+_b[x`i']*x`i'
}

replace vijnaive = vijnaive+_b[z]*z
*Create estimate of dv1dz1

 gen dv1dz1est = _b[z]

local vij = "vijnaive"
local label = "naive"
do programs/makelogitprob.do "ID" "vij" "`label'" "`J'" "altID"


*Adjust weights so that denominator is never too small causing weights to blow up
replace sijnaive = max(sijnaive,.1)
replace sijnaive = min(sijnaive,0.9)

forvalues k = 1/`J' {
gen si`k'naive = sijnaive if altID == `k'
bys ID: egen si`k'maxnaive = max(si`k'naive)
drop si`k'naive
rename si`k'maxnaive si`k'naive
}

forvalues k = 1/`J' {

gen bx_`k' = si`k'naive/(1-sijnaive)

gen bz_`k' = si`k'naive/(1-sijnaive)

gen b2x_`k' = dv1dz1est*(1-2*sijnaive)*si`k'naive/((1-sijnaive)*(1+dv1dz1est*(1-2*sijnaive)*z))

gen b2z_`k' = dv1dz1est*(1-2*sijnaive)*si`k'naive/((1-sijnaive)*(1+dv1dz1est*(1-2*sijnaive)*z))

gen b2test_`k' = dv1dz1est*(1-2*sijnaive)*si`k'naive/(1-sijnaive)


*Adjust weights so that denominator is never too small causing weights to blow up

gen newval_`k' = 1+dv1dz1est*(1-2*sijnaive)*z
gen absnew_`k' = abs(newval_`k')
replace b2x_`k' = b2test_`k' if absnew_`k' <= 1
replace b2z_`k' = b2test_`k' if absnew_`k' <= 1
drop newval_`k' absnew_`k'

}


do programs/makevariablessym.do `J'



clogit chosen x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 z zzmax zsum zzsum zxsum1 zxsum2 zxsum3 zxsum4 zxsum5 xsum1 xsum2 xsum3 xsum4 xsum5 , group(ID)




forvalues i=1/3{
local gammax`i' = _b[xsum`i']
local deltax`i' = _b[zxsum`i']
}

local gammaz = _b[zsum]
local deltaz = _b[zzsum]


local num = -_b[z]+`gammaz'+`deltaz'
forvalues i=1/3 {
local denom`i' = -_b[x`i']+`gammax`i''+`deltax`i''
local theoryrat`i' = `num'/`denom`i''
}

forvalues i=1/3 {
local impliedbeta`i'=`theoryrat`i''*`alpha`i''
}


keep if _n == 1
keep chosen

forvalues i=1/3 {
gen naivex`i' = `naivex`i''
gen alpha`i'=`alpha`i''
gen impliedbeta`i'=`impliedbeta`i''
}
forvalues i=4/14{
gen alpha`i'=`alpha`i''
}
gen naivez=`naivez'
drop chosen



